package gui.ParkingLotWorker;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.ParkingFloor;
import vcpClasses.ParkingLocation;
import vcpClasses.ParkingLot;
import vcpClasses.ParkingRow;
import vcpClasses.ParkingSpace;
import vcpClasses.Worker;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;

import control.LocalOpertaionController;
import control.LoginController;
/**
 * this class use to Set Parking Status Screen
 * @author G8
 *
 */
public class SetParkingStatusScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnSetStatus;
	private JLabel lblFloorNumber;
	private JLabel lblRowNumber;
	private JLabel lblSpaceNumber;
	private JCheckBox chckbxEntireLot;
	private JCheckBox chckbxEntireFloor;
	private JCheckBox chckbxEntireRow;
	private JComboBox comboBoxFloorNumber;
	private JComboBox comboBoxRowNumber;
	private JComboBox comboBoxSpaceNumber;
	private JComboBox comboBoxStatus;
	private SystemTypes deviceSetResult;
	private LocalOpertaionController localOpControl;
	private SystemTypes deviceToSet;
	/**
	 *Set Parking Status Screen Constructor 
	 */
	public SetParkingStatusScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	public void initialize(){
		localOpControl = new LocalOpertaionController(null);
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Set Parking Status", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(InitializeLotScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnSetStatus = new JButton("Set Status");
		btnSetStatus.setBounds(206, 284, 110, 23);
		btnSetStatus.addActionListener(this);
		add(btnSetStatus);
		
		JLabel lblLotNumber = new JLabel("Lot Number:");
		lblLotNumber.setBounds(105, 122, 110, 14);
		add(lblLotNumber);
		
		lblFloorNumber = new JLabel("Floor Number:");
		lblFloorNumber.setBounds(105, 151, 110, 14);
		add(lblFloorNumber);
		
		lblRowNumber = new JLabel("Row Number:");
		lblRowNumber.setBounds(105, 179, 100, 14);
		add(lblRowNumber);
		
		lblSpaceNumber = new JLabel("Space Number:");
		lblSpaceNumber.setBounds(105, 204, 110, 14);
		add(lblSpaceNumber);
		
		comboBoxFloorNumber = new JComboBox();
		comboBoxFloorNumber.setBounds(227, 149, 89, 20);
		for (int i=1;i<4;i++)
			comboBoxFloorNumber.addItem(i);
		add(comboBoxFloorNumber);
		
		comboBoxRowNumber = new JComboBox();
		comboBoxRowNumber.setBounds(227, 177, 89, 20);
		for (int i=1;i<4;i++)
			comboBoxRowNumber.addItem(i);
		add(comboBoxRowNumber);
		
		comboBoxSpaceNumber = new JComboBox();
		comboBoxSpaceNumber.setBounds(227, 205, 89, 20);
		System.out.println(VCP.currentLot.getNumOfSpacesInRow());
		for (int i=1;i<VCP.currentLot.getNumOfSpacesInRow()+1;i++)
			comboBoxSpaceNumber.addItem(i);
		add(comboBoxSpaceNumber);
		
		JLabel lblPleaseChooseThe = new JLabel("Please choose the parking space you want to set.");
		lblPleaseChooseThe.setBounds(105, 83, 307, 14);
		add(lblPleaseChooseThe);
		
		JLabel lblStatus = new JLabel("Status:");
		lblStatus.setBounds(105, 255, 78, 14);
		add(lblStatus);
		
		comboBoxStatus = new JComboBox();
		comboBoxStatus.setBounds(227, 253, 89, 20);
		comboBoxStatus.addItem("Available");
		comboBoxStatus.addItem("Maintenance");
		comboBoxStatus.addItem("Damaged");
		comboBoxStatus.addItem("Reserved");
		add(comboBoxStatus);
		
		chckbxEntireFloor = new JCheckBox("Entire Floor");
		chckbxEntireFloor.setBounds(331, 148, 97, 23);
		chckbxEntireFloor.addActionListener(this);
		add(chckbxEntireFloor);
		
		chckbxEntireRow = new JCheckBox("Entire Row");
		chckbxEntireRow.setBounds(331, 176, 97, 23);
		chckbxEntireRow.addActionListener(this);
		add(chckbxEntireRow);
		
		chckbxEntireLot = new JCheckBox("Entire Lot");
		chckbxEntireLot.setBounds(331, 119, 97, 23);
		chckbxEntireLot.addActionListener(this);
		add(chckbxEntireLot);
		
		JLabel lblCurrLot = new JLabel("currentLot");
		lblCurrLot.setBounds(225, 122, 91, 14);
		lblCurrLot.setText(Integer.toString(VCP.currentLot.getParkingLotNumber()));
		add(lblCurrLot);
		
		
		
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
@Override
	public void actionPerformed(ActionEvent e) {
	if (e.getSource() == chckbxEntireLot){
		if (chckbxEntireLot.isSelected()==true){
			chckbxEntireFloor.setEnabled(false);
			chckbxEntireRow.setEnabled(false);
			comboBoxFloorNumber.setEnabled(false);
			comboBoxRowNumber.setEnabled(false);
			comboBoxSpaceNumber.setEnabled(false);
		}
		else{
			chckbxEntireFloor.setEnabled(true);
			chckbxEntireRow.setEnabled(true);
			comboBoxFloorNumber.setEnabled(true);
			comboBoxRowNumber.setEnabled(true);
			comboBoxSpaceNumber.setEnabled(true);
		}
	}
	if (e.getSource() == chckbxEntireFloor){
		if (chckbxEntireFloor.isSelected()==true){
			chckbxEntireLot.setEnabled(false);
			chckbxEntireRow.setEnabled(false);
			comboBoxRowNumber.setEnabled(false);
			comboBoxSpaceNumber.setEnabled(false);
		}
		else{
			chckbxEntireLot.setEnabled(true);
			chckbxEntireRow.setEnabled(true);
			comboBoxRowNumber.setEnabled(true);
			comboBoxSpaceNumber.setEnabled(true);
		}
	}
	if (e.getSource() == chckbxEntireRow){
		if (chckbxEntireRow.isSelected()==true){
			chckbxEntireLot.setEnabled(false);
			chckbxEntireFloor.setEnabled(false);
			comboBoxSpaceNumber.setEnabled(false);
		}
		else{
			chckbxEntireLot.setEnabled(true);
			chckbxEntireFloor.setEnabled(true);
			comboBoxSpaceNumber.setEnabled(true);
		}
	}
	if (e.getSource() == btnSetStatus){
		SystemMode deviceMode=null;
		if (comboBoxStatus.getSelectedItem().toString().equals("Available"))
			deviceMode=SystemMode.SET_FREE;
		if (comboBoxStatus.getSelectedItem().toString().equals("Maintenance"))
			deviceMode=SystemMode.SET_MAINTENANCE;
		if (comboBoxStatus.getSelectedItem().toString().equals("Damaged"))
			deviceMode=SystemMode.SET_DAMAGED;
		if (comboBoxStatus.getSelectedItem().toString().equals("Reserved"))
			deviceMode=SystemMode.SET_SAVED;
		if (chckbxEntireLot.isSelected()==true){
			deviceToSet = new SystemTypes(VCP.currentLot,deviceMode);
		}
		else if (chckbxEntireFloor.isSelected()==true){
			ParkingFloor newParkingFloor = new ParkingFloor(VCP.currentLot.getParkingLotNumber(),(int)comboBoxFloorNumber.getSelectedItem());
			deviceToSet = new SystemTypes(newParkingFloor,deviceMode);
		}
		else if (chckbxEntireRow.isSelected()==true){
			ParkingRow newParkingRow = new ParkingRow((int)comboBoxRowNumber.getSelectedItem(),(int)comboBoxFloorNumber.getSelectedItem(),VCP.currentLot.getParkingLotNumber());
			deviceToSet = new SystemTypes(newParkingRow,deviceMode);
		}
		else{
			ParkingLocation newParkingSpace = new ParkingLocation(VCP.currentLot.getParkingLotNumber(),(int)comboBoxFloorNumber.getSelectedItem(),(int)comboBoxRowNumber.getSelectedItem(),(int)comboBoxSpaceNumber.getSelectedItem());
			deviceToSet = new SystemTypes(newParkingSpace,deviceMode);
		}
		deviceSetResult = localOpControl.setDeviceDisableStatus(deviceToSet);
		if (deviceToSet.getSysMode()==SystemMode.OK){
			JOptionPane.showMessageDialog(null, deviceSetResult.getObj().toString(), "Success",
                    JOptionPane.PLAIN_MESSAGE);
		}
		else{
			JOptionPane.showMessageDialog(null, deviceSetResult.getObj().toString(), "Error",
                    JOptionPane.ERROR_MESSAGE);
		}
	}
	if (e.getSource() == btnBack){
		VCP.setScreen(this, VCP.prevScreen);
		VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
		VCP.prevScreen.setLayout(null);
		VCP.frame.setBounds(VCP.prevScreen.getBounds());
	}
	if (e.getSource() == btnLogout){
		SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
		boolean logoutRes = LoginController.LogOut(userToLogout);
		if (logoutRes)
			VCP.setGUI(0, this);
		else{
			JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
                    JOptionPane.ERROR_MESSAGE);
		}
	}
	}
}
